<?php
/**
 * Created by PhpStorm.
 * User: HeiYanHeiMao
 * Date: 2020/2/27
 * Time: 17:44
 */

namespace app\controller;


use app\BaseController;
use app\middleware\Check;
use app\model\GoodsModel;
use app\Request;
use think\facade\Validate;
use think\validate\ValidateRule;
use \think\response\Json;
/**商品管理
 * Class Goods
 * @package app\controller
 */
class Goods extends BaseController
{
    protected $middleware = [Check::class];

    /**商户添加商品
     * @param Request $request
     * @return Json
     */
    public function add(Request $request)
    {
        if (!$request->isPost()) return jsonFail('非法请求方式');
        // 接收参数
        $input['cate_id'] = $request->post('cate_id'); // 商品分类id
        $input['type'] = $request->post('type',1); // 1 普通类型 2套餐类型
        $input['meat_num'] = $request->post('meat_num',0); // 荤菜个数
        $input['veg_num'] = $request->post('veg_num',0); // 素菜个数
        $input['name'] = $request->post('name'); // 商品名称
        $input['pic'] = $request->post('pic'); // 商品图片
        $input['price'] = $request->post('price'); // 商品单价
        $input['detail'] = $request->post('detail'); // 描述
        $input['stock'] = $request->post('stock'); // 库存
        //验证参数
        $validate = Validate::rule([
            'cate_id' => ValidateRule::isRequire(null, '缺少参数'),
            'name' => ValidateRule::isRequire(null, '缺少参数'),
            'pic' => ValidateRule::isRequire(null, '缺少参数'),
            'price' => ValidateRule::isRequire(null, '缺少参数'),
            'detail' => ValidateRule::isRequire(null, '缺少参数'),
            'stock' => ValidateRule::isRequire(null, '缺少参数'),
        ]);

        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        // 逻辑处理
        return GoodsModel::add($input, $request->param('token'));
    }

    /**获取列表
     * @param Request $request
     * @return Json
     */
    public function list(Request $request)
    {
        if (!$request->isPost()) return jsonFail('非法请求方式');
        // 接收参数
        $input['page'] = $request->post('page','1');
        $input['limit'] = $request->post('limit','10');
        $input['name'] = $request->post('name','');
        $input['shop_name'] = $request->post('shop_name','');
        $input['type'] = $request->post('type','');
        //验证参数
        $validate = Validate::rule([
            'type' => function($v) {
                return in_array($v, ['','1','2'],true) ? true : '不合法数据';
            },
            'limit' => function($v) {
                return in_array($v, ['10','20','30','40'],true) ? true : '不合法数据';
            },
            'page' => ValidateRule::regex('/^[1-9]\d*$/','不合法数据')
        ]);

        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        // 逻辑处理
        return GoodsModel::list($input, $request->param('token'));
    }

    /** 设置餐盒费
     * @param Request $request
     * @return Json
     */
    public function setFee(Request $request)
    {
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $input['id'] = $request->post('id','0');
        $input['lunchbox_fee'] = $request->post('lunchbox_fee','0');
        // 参数校验
        $validate = Validate::rule([
            'id' => ValidateRule::regex('/^[1-9]\d*$/','不合法数据'),
            'lunchbox_fee' =>  ValidateRule::isRequire(null, '缺少参数')->regex("/^(([0-9]\d?)|(\d\.\d{1,2})|([1-9]\d\.\d{1,2}))$/",'餐盒费不正确'),
        ]);

        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        // 逻辑处理
        return GoodsModel::setFee($input, $request->post('token'));
    }

    /** 餐盒费收取方设置
     * @param Request $request
     * @return Json
     */
    public function typeChange(Request $request)
    {
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $input['id'] = $request->post('id','0');
        $input['lunchbox_type'] = $request->post('lunchbox_type','1');
        // 参数校验
        $validate = Validate::rule([
            'id' => ValidateRule::regex('/^[1-9]\d*$/','不合法数据'),
            'lunchbox_type' => function($v) {
                return in_array($v, ['1', '2'], true) ? true : '非法参数';
            },
        ]);

        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        // 逻辑处理
        return GoodsModel::typeChange($input, $request->post('token'));
    }
}